﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using BusinessObjectLayer;
using BusinessLogicLibrary;
using BusinessLogicLayer;

namespace UserInterface
{
    public partial class ImageUserControl : UserControl
    {
        int i = 0;
        int CtrlWidth;
        int CtrlHeight;
        int PicWidth;
        int PicHeight;
        int XLocation;
        int YLocation;
        string _Directory_Path;

        public string Directorypath
        {
            get { return _Directory_Path; }
            set
            {
                _Directory_Path = value;
                XLocation = 25;
                YLocation = 25;
                PicWidth = 117;
                PicHeight = 109;
                CreateGallery();
            }
        }
        public ImageUserControl()
        {
            InitializeComponent();
        }
        private void DrawPictureBox(string _filename, string _displayname)
        {
            PictureBox picturBox = new PictureBox();
            picturBox.Location = new System.Drawing.Point(XLocation, YLocation);
            XLocation = XLocation + PicWidth + 20;
            if (XLocation + PicWidth >= CtrlWidth)
            {
                XLocation = 25;
                YLocation = YLocation + PicHeight + 20;
            }
            picturBox.Name = "PictureBox" + i;
            i += 1;
            picturBox.Size = new System.Drawing.Size(PicWidth, PicHeight);
            picturBox.TabIndex = 0;
            picturBox.TabStop = false;
            picturBox.BorderStyle = BorderStyle.Fixed3D;
            this.toolTip1.SetToolTip(picturBox, _displayname);
            picturBox.MouseEnter += Pic1_MouseEnter;
            picturBox.MouseLeave += Pic1_MouseLeave;
            this.Controls.Add(picturBox);
            picturBox.Image = Image.FromFile(_filename);
            picturBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
        }
        private void CreateGallery()
        {
            i = 0;
            RemoveControls();
            if (Directorypath != null)
            {
                string path = string.Format("{0}/Gallery", ServerBLL.GetStringValue("TempPath"));
                if (Directory.Exists(path))
                    Directory.Delete(path, true);                
                Directory.CreateDirectory(path);
                FileBOL _FileBOL = new FileBOL(Directorypath);
                new FileBLL().DownloadFiles(_FileBOL);
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                FileInfo[] jpgFiles = directoryInfo.GetFiles("*.jpg");
                FileInfo[] bmpFiles = directoryInfo.GetFiles("*.bmp");
                FileInfo[] pngFiles = directoryInfo.GetFiles("*.png");
                FileInfo[] gifFiles = directoryInfo.GetFiles("*.gif");
                var fileList = new List<FileInfo>();
                fileList.AddRange(jpgFiles);
                fileList.AddRange(bmpFiles);
                fileList.AddRange(pngFiles);
                fileList.AddRange(gifFiles);
                FileInfo[] fileInfo = fileList.ToArray();
                foreach (FileInfo _FileInfo in jpgFiles)
                    DrawPictureBox(_FileInfo.FullName, _FileInfo.Name);
            }
        }
        private void Pic1_MouseEnter(System.Object sender, System.EventArgs e)
        {
            PictureBox Pic = default(PictureBox);
            Pic = (PictureBox)sender;
            Pic.BorderStyle = BorderStyle.FixedSingle;
        }
        private void Pic1_MouseLeave(System.Object sender, System.EventArgs e)
        {
            PictureBox Pic = default(PictureBox);
            Pic = (PictureBox)sender;
            Pic.BorderStyle = BorderStyle.Fixed3D;
        }
        private void RemoveControls()
        {
        Again:
            foreach (Control ctrl in this.Controls)
            {
                if ((ctrl) is PictureBox)
                {
                    this.Controls.Remove(ctrl);
                }
            }
            if (this.Controls.Count > 0)
            {
                goto Again;
            }
        }
    }
}
